package cn.itcast.order.web;

import cn.itcast.order.pojo.Order;
import cn.itcast.order.service.OrderService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @Value("${order.param1}")
    private String param1;

    @Value("${order.param2}")
    private String param2;


    @GetMapping("/feign/{orderId}")
    public Order queryOrderFeign(@PathVariable("orderId") Long orderId) {
        return orderService.queryOrderFeign(orderId);
    }


    @GetMapping("/restTemplate/{orderId}")
    public Order queryOrderRestTemplate(@PathVariable("orderId") Long orderId) {
        return orderService.queryOrderRestTemplate(orderId);
    }

    @SentinelResource(value = "hot"/*, blockHandler = "fallbackHandler", blockHandlerClass = OrderController.class*/)
    @GetMapping("/{orderId}")
    public Order queryOrder(@PathVariable("orderId") Long orderId) {
//        return orderService.queryOrderFeign(orderId);
        return orderService.queryOrder(orderId);
    }

    @GetMapping("/update")
    public String update() {
        return "order-update";
    }


    @GetMapping("/query")
    public String query() {
        orderService.queryGoods();
        System.out.println("order-query");
        return "order-query";
    }


    @GetMapping("/save")
    public String save() {
        orderService.queryGoods();
        System.out.println("order-save");
        return "order-save";
    }


    //优先级[ 优先级从高到低]：
    // order-service-dev.yml
    // > order-service.yml
    // > application-dev.yml
    // >application.yml
    // > bootstrap.yml
    // 优先级高的覆盖优先级低的
    @GetMapping("/config")
    public String config() {
        return "param1:" + param1 + "\n" +
                "param2:" + param2;
    }

    public static String fallbackHandler(Long orderId) {
        String msg = "不好意思，前方拥挤，请您稍后再试";
        return msg;
    }

    @Value("${access.key}")
    private String accessKey;

    @GetMapping("/config/share")
    public String configShare() {
        return accessKey;
    }


}
